﻿<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>机构人员管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link href="/lobsteruiframe/layuiadmin/layui/css/layui.css" rel="stylesheet" />
    <link href="/lobsteruiframe/layuiadmin/style/admin.css" rel="stylesheet" />
    <script src="/lobsteruiframe/layuiadmin/layui/layui.js"></script>
    <link rel="stylesheet" type="text/css" href="/lobsteruiframe/easyui/themes/gray/easyui.css">
    <script src="/jslib/jquery-3.4.1.min.js"></script>
    <script src="/lobsteruiframe/easyui/jquery.easyui.min.js"></script>
    <script src="/lobsteruiframe/easyui/locale/easyui-lang-zh_CN.js"></script>
    <script src="/jslib/lobster.js"></script>
    <style>
        /*easyui combobox下拉框样式*/
        .textbox {
            -webkit-border-radius: 0;
            border-radius: 0;
            height: 38px;
        }

        .combo-arrow {
            height: 38px;
        }

        .textbox .textbox-text {
            height: 38px;
        }
        /*easyui combobox下拉框样式*/
    </style>
</head>
<body>
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                    <div class="layui-input-inline" id="divWorker">
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width:100px">医疗机构：</label>
                            <div class="layui-input-inline">
                                <select name="workId" id="workId" lay-filter="workId"></select>
                            </div>
                        </div>
                    </div>
                    <div class="layui-input-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label">科室：</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="deptId" name="deptId" />
                            </div>
                        </div>
                    </div>
                    <div class="layui-input-inline">
                        <div class="layui-form-item">
                            <label class="layui-form-label" style="width:100px">关键字：</label>
                            <div class="layui-input-inline">
                                <input type="text" id="username" name="username" placeholder="请输入姓名,手机号,身份证号"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-input-inline">
                        <div class="layui-form-item">
                            <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search" id="btnSearch">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>查询
                            </button>
                        </div>
                    </div>
            </div>
            <div class="layui-card-body">
                <script id="toolbar" type="text/html">
                    <div class="layui-btn-group">
                        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-1"></i>添加</button>
                    </div>
                </script>
                <table id="EmpInfoTable" lay-filter="EmpInfoTable"></table>
            </div>
        </div>
    </div>
</body>
</html>
<script type="text/javascript">
    lobsterlayui.main(['index', 'layer', 'form', 'element', 'table', 'laydate', 'upload', 'tree'], {
        data: {
            $layui: null,
            right: null,
            tableemp: null,
            selectdeptId:0,
        },
        pageload: function (right) {
            this.data.right = right;
            this.loadselectworker(() => {
                this.loadselectdept();
                this.loadtableemp();
            })
            if (right.IsAdmin == 0) {
                $('#divWorker').hide();
            }
            this.initevent();
        },
        initevent() {
            var self = this;
            layui.form.on('select(workId)', function (data) {
                console.log(data);
                self.loadDept();
            });
            //查找reload,通过条件，查询网格的table重载数据显示到界面
            layui.form.on("submit(LAY-user-front-search)", function (data) {

                self.data.tableemp.reload({
                    where: {
                        workId: $('#workId').val(),
                        deptId: self.data.selectdeptId,
                        username: $('#username').val()
                    },
                    page: {
                        curr: 1
                    }
                });
            });

            //添加按钮的点击事件
            layui.table.on('toolbar(EmpInfoTable)', function (obj) {

                //添加人员信息
                if (obj.event == "add") {
                    
                    layer.open({
                        type: 1
                        , title: '添加人员详细信息'
                        , content: $('#dlg_addemp').html()
                        , maxmin: true
                        , area: ['100%', '100%']  //宽，高
                        , btn: ['保存', '取消']
                        , cancel: function () {
                            $("#LAY-emp-form-reset").trigger('click');
                        }
                        //1.第一次进入界面展示，只有展示保存0，取消7按钮。
                        , success: function (layero, index) {
                            $("#EmpId").val(0);
                            $('#WorkId').val($('#workId').val());
                            empform.initpage();
                        }

                        //第一个保存按钮 btn0 btnSaveEmpFirst
                        , yes: function (index, layero) {
                            //保存人员及查询方法，人员转到医生界面
                            //FunYes(index, layero);
                            var submit = $('#btnSubmit')
                            //监听提交 1. 人员信息的单独保存方法
                            layui.form.on('submit(btnSubmit)', function (formdata) {
                                empform.SaveEmpsData(formdata, () => {
                                    self.data.tableemp.reload();
                                });
                            });
                            submit.trigger('click');
                        }

                    });
                }
                
            });

            //监听行工具事件
            layui.table.on('tool(EmpInfoTable)', function (obj) {  //tool是工具条事件名，UserInfoTable是table原始容器的属性
                var data = obj.data;//当前行数据
                var tr = obj.tr;
                var layEvent = obj.event;//获得lay-event的值
                
                //编辑
                if (layEvent === "edit") {
                    layer.open({
                        type: 1
                        , title: '编辑人员信息'
                        , content: $('#dlg_addemp').html()
                        , area: ['100%', '100%']  //宽，高
                        , btn: ['保存', '取消']
                        , success: function (layero, index) { 
                            $("#EmpId").val(data.EmpId);
                            empform.initpage();
                        }

                        , cancel: function () {
                            $("#LAY-emp-form-reset").trigger('click');
                        }
                        //对应第一个按钮 即保存按钮
                        , yes: function (index, layero) {
                            var submit = $('#btnSubmit')
                            //监听提交 1. 人员信息的单独保存方法
                            layui.form.on('submit(btnSubmit)', function (formdata) {
                                empform.SaveEmpsData(formdata, () => {
                                    self.data.tableemp.reload();
                                });
                            });
                            submit.trigger('click');
                        }
                        , btn2: function (index, layero) {
                            $("#LAY-emp-form-reset").trigger('click');
                        }
                    });
                }
                //启用或停用
                if (layEvent === "disable" || layEvent === "enable") {
                    self.StopEmp(data);
                }
                //关联科室
                if (layEvent === "reldept") {
                    //关联科室
                    layer.open({
                        type: 1,
                        title: "关联科室",
                        //btn: ["确认科室", "取消"],
                        btnAlign: "r",
                        content: $('#dlg_reldept').html(),
                        area: ['650px', '500px'],  //宽，高
                        success: function (layero, index) {
                            reldept.initpage(data);
                        },
                        cancel: function () {
                            //tr.removeAttr("class");
                            $("#LAY-emp-form-reset").trigger('click');
                        }
                    });
                }
                //授权应用
                if (layEvent === "authapp") {
                    layer.open({
                        type: 1
                        , title: '编辑人员信息['+data.Name+']'
                        , content: $('#dlg_authapp').html()
                        , area: ['320px', '480px']  //宽，高
                        , btn: ['保存', '取消']
                        , success: function (layero, index) {
                            authapp.initpage(data);
                        }
                        , cancel: function () {

                        }
                        //对应第一个按钮 即保存按钮
                        , yes: function (index, layero) {
                            authapp.savetreeapprole(data);
                        }
                        , btn2: function (index, layero) {

                        }
                    });
                }
            });
        },
        loadselectworker(callback) {
            var self = this;
            //获取机构数据
            lobsterlayui.GET('/admin/v1/worker/GetSelectWorkerDataByUser').then(data => {
                //医疗机构
                var workers = data["Workers"];
                $("#workId").html("");
                $.each(workers, function (i, item) {
                    $("#workId").append("<option value=\"" + item.WorkId + "\">" + item.WorkName + "</option>");
                });

                $('#workId').val(self.data.right.WorkId);//默认选择当前登录机构
                //重新渲染
                layui.form.render("select");

                if (callback) {
                    callback();
                }
            })
        },
        loadselectdept(){
            var self = this;
            $('#deptId').combotree({
                url: '/admin/v1/emp/GetAllDeptTree?workId=' + $('#workId').val(),
                //headers: lobsterlayui.GetHeaderData(),
                method: 'get',
               // data: { workId: 1 },//self.data.search.workId
                loadFilter: function (res) {
                    console.log(res);
                    var data = res.data.data;
                    data.forEach(a => {
                        a.text = a.name;
                        //a.id = "n-" + a.id;
                        a.id = "";
                        a.children.forEach(b => {
                            b.text = b.name;
                            //b.id = "l-" + b.id;
                            b.id = "";
                            b.children.forEach(c => {
                                c.text = c.name;
                            })
                        })
                    })
                    data.splice(0, 0, { id: "", text: "--全部--" });
                    return data;

                }, onSelect: function (node) {
                    console.log(node)
                    var id = node.id;
                    self.data.selectdeptId = id;
                }
            });
        },
        loadtableemp() {
            var self = this;
            this.data.tableemp = layui.table.render({
                elem: '#EmpInfoTable',
                headers: lobsterlayui.GetHeaderData(),
                where: {
                    workId: $('#workId').val(),
                    deptId: self.data.selectdeptId,
                    username: $('#username').val()
                },
                height: 'full-130',
                cellMinWidth: 80,
                toolbar: '#toolbar',
                url: '/admin/v1/emp/GetEmpData',
                page: true,
                //even: true,
                cols: [[
                    { field: 'EmpId', title: 'ID', width: '70', fixed: 'left' },
                    { field: 'Name', title: '姓名', width: '8%', sort: true, fixed: 'left' },
                    { field: 'StrSex', title: '性别', width: '5%' },
                    { field: 'IDNumber', title: '身份证号码', width: '15%', templet: '#IDNumberTpl' },
                    { field: 'Telephone', title: '电话', width: '10%', templet: '#TelephoneTpl' },
                    { field: 'Brithday', title: '出生日期', width: '10%', templet: '#dateTpl' },
                    { field: 'DeptName', title: "默认科室", width: "10%" },
                    { field: 'UserTypeName', title: "人员类别", width: "8%" },
                    { field: 'StrDelFlag', title: '状态标识', width: "8%", sort: true, align: 'center', templet: '#flagTpl' },
                    { title: '操作', align: 'left', toolbar: '#useroption', width: "350", fixed: 'right' }   //操作script的id 
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.data
                    }
                }
            });
        },
        StopEmp(data) {
            var self = this;
            // 启用停用
            var confirm_msg = "";
            var status = 0;
            if (data.DelFlag == 0) {
                confirm_msg = "确认停用么?";
                status = 1;
            }
            else {
                confirm_msg = "确认启用么?";
                status = 0;
            }

            layer.confirm(confirm_msg, { icon: 3, title: "提示" },
                function (index) {

                    lobsterlayui.GET('/admin/v1/emp/stopemp', { empid: data.EmpId, status: status }).then(data => {
                        layer.close(index); //关闭弹层
                        self.data.tableemp.reload();
                    })
                });
        }
    })
    //添加修改科室
    var empform = {
        datarank: {},
        initpage() {
            var self = this;
            this.InitEmpBasicSelect(function (obj) {
                self.InitEmpData(obj);
            });
            this.initevent();
            this.uploadImg();
        },
        //在身份证号码input失去焦点时检查验证
        checkCardNo(sId) {
            var len = sId.length, re;
            var aCity = {
                11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海",
                32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南",
                44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃",
                63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
            }
            if (len > 0) {
                var iSum = 0;
                if (!/^\d{17}(\d|X|x)$/i.test(sId)) {
                    layer.msg('你输入的身份证长度或格式错误!', { icon: 5, ime: 1000, });
                }
                sId = sId.replace(/x$/i, "a");
                sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
                $("#Brithday").val(sBirthday);  //填完身份证号，带出出生日期
                var d = new Date(sBirthday.replace(/-/g, "/"));
                for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
                if (aCity[parseInt(sId.substr(0, 2))] == null) {
                    layer.msg('你的身份证地区非法!', { icon: 5, ime: 1000, });
                } else if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) {
                    layer.msg('身份证上的出生日期非法!', { icon: 5, ime: 1000, });
                }
                else if (iSum % 11 != 1) {
                    layer.msg('你输入的身份证号非法!', { icon: 5, ime: 1000, });
                }
                if (parseInt(sId.substr(16, 1)) % 2 == 1) {
                    $("#Sex").val(1);//男
                }
                else {
                    $("#Sex").val(2);//女
                }
                layui.form.render();
            }
        },
        //初始化事件
        initevent: function () {
            var self = this;
            //人员信息的日期
            layui.laydate.render({
                elem: '#Brithday', trigger: 'click'
            });
            $("#IDNumber").blur(function () {
                var sId = $("#IDNumber").val();
                //1.验证身份证号码
                self.checkCardNo(sId);
            });
            //自定义验证规则
            layui.form.verify({
                chkIDNumber: function (sId) {
                    //checkCardNo(sId);
                    var len = sId.length, re;
                    var aCity = {
                        11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海",
                        32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北", 43: "湖南",
                        44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃",
                        63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
                    }
                    if (len > 0) {
                        var iSum = 0;
                        if (!/^\d{17}(\d|X|x)$/i.test(sId)) return "你输入的身份证长度或格式错误";
                        sId = sId.replace(/x$/i, "a");
                        if (aCity[parseInt(sId.substr(0, 2))] == null) return "你的身份证地区非法";
                        sBirthday = sId.substr(6, 4) + "-" + Number(sId.substr(10, 2)) + "-" + Number(sId.substr(12, 2));
                        var d = new Date(sBirthday.replace(/-/g, "/"));
                        if (sBirthday != (d.getFullYear() + "-" + (d.getMonth() + 1) + "-" + d.getDate())) return "身份证上的出生日期非法";
                        for (var i = 17; i >= 0; i--) iSum += (Math.pow(2, i) % 11) * parseInt(sId.charAt(17 - i), 11);
                        if (iSum % 11 != 1) return "你输入的身份证号非法";
                    } else {
                        return "请输入你的身份证号码！";
                    }
                },
                chkphone: function (value) {
                    var len = value.length, re;
                    if (len > 0) {
                        if (len == 7)
                            re = new RegExp(/^([0-9]|[-])+$/g);
                        else if (len == 11)
                            re = new RegExp(/^[1][3-8]+\\d{9}/);
                        else {
                            return '输入的电话位数不对';
                        }
                    } else {
                        return '请输入你的电话号码';
                    }
                },
                checkemail: function (value) {
                    var len = value.length, re;
                    if (len > 0) {
                        re = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
                        if (!re.test(value)) {
                            return '邮箱格式不对';
                        }
                    }
                }
            });

            layui.form.on('select(EmpType)', function (data) {
                $("#RankCode").html("");
                if (data.value == 1 || data.value == 2) {
                    $.each(self.datarank.data["dtDoctorCode"], function (i, item) {
                        $("#RankCode").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                    });
                } else if (data.value == 3) {
                    $.each(self.datarank.data["dtPharmacistCode"], function (i, item) {
                        $("#RankCode").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                    });
                } else {
                    $.each(self.datarank.data["dtNurseCode"], function (i, item) {
                        $("#RankCode").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                    });
                }
                layui.form.render("select");
            });

        },

        //获取基础数据
        InitEmpBasicSelect: function (callback) {
            var self = this;
            //console.log(currentData);
            

            //重新渲染  
            layui.form.render("select");

            var param = {
                data: JSON.stringify(["dtSex", "dtUserType", "dtDoctorCode", "dtPharmacistCode",'dtNurseCode'])
            }

            lobsterlayui.GET('/basic/v1/BasicData/GetDictData', param).then(data => {
                //获取性别信息
                var sex = data["dtSex"]
                $("#Sex").html("");
                $.each(sex, function (i, item) {
                    $("#Sex").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                });

                //获取人员类别 
                var userType = data["dtUserType"]
                $("#EmpType").html("");
                $.each(userType, function (i, item) {
                    $("#EmpType").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                });

                //重新渲染  
                layui.form.render("select");

                if (callback) {
                    self.datarank = data;
                    callback(data);
                }
            })

        },

        //编辑人员信息时，通过人员Id获取具体的人员资料
        InitEmpData: function (data) {
            var empId = $("#EmpId").val();
            if (empId > 0) {
                lobsterlayui.GET('/admin/v1/emp/GetEmpDetail', { empId: empId }).then(retdata => {
                    //给界面赋值:根据EmpId人员ID，获取人员详细信息 EmpDetails               
                    var empdetails = retdata["EmpDetails"];
                    //医生职称
                    var dtrankcode = data["dtDoctorCode"];
                    if (empdetails.EmpType == 3)
                        dtrankcode = data["dtPharmacistCode"];
                    if (empdetails.EmpType == 4)
                        dtrankcode = data["dtNurseCode"];
                    $("#RankCode").html("");
                    $.each(dtrankcode, function (i, item) {
                        $("#RankCode").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                    });
                    layui.form.val('formEmps', empdetails);
                    $("#UPic").val(empdetails.UPic || '/resource/service.jpg');
                    $("#imgUpload").attr('src', empdetails.UPic || '/resource/service.jpg');
                    layui.form.render();
                    console.log(empdetails)
                })
            } else { 
                var dtrankcode = data["dtDoctorCode"];
                $("#RankCode").html("");
                $.each(dtrankcode, function (i, item) {
                    $("#RankCode").append("<option value=\"" + item.Code + "\">" + item.Name + "</option>");
                });
                layui.form.render();
            }
        },

        //保存人员信息
        SaveEmpsData: function (formdata,callback) {
            var field = formdata.field;     //获取提交的字段
            field.RankName = $("#RankCode").val();
            if (field && field.UPic == '/resource/service.jpg') {
                field.UPic = '';
            }
            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
            //检查是否存在此身份证号
            lobsterlayui.POST('/admin/v1/emp/SaveEmpsData', field).then(retdata => {
                layer.close(load);
                layer.closeAll();
                if (callback) callback();
            })
            
        },

        //上传图片
        uploadImg: function () {
            layui.upload.render({
                elem: "#btnUpload",
                size: 1024,
                url: "/basic/v1/OSS/UploadFile",
                done: function (res) {
                    if (res.code == 0) {
                        $('#imgUpload').attr("src", res.data.url); //图片链接（base64）
                        $("#UPic").val(res.data.url);
                    }
                }
            });
        }
    }
    //关联科室
    var reldept = {
        tablereldept: null,
        initpage(data) {
            this.loadselectdept(data.WorkId);
            this.loadtablereldept(data.EmpId);
            this.initevent(data);
        },
        initevent(data) {
            var self = this;
            var empid = data.EmpId;
            var workid = data.WorkId;
            //添加科室
            layui.form.on("submit(btnAddDept)", function (data) {
                var deptid = $('#dlg_deptId').val();
                lobsterlayui.GET('/admin/v1/emp/EmpAddDept', { empId: empid, deptId: deptid, workId: workid }).then(retdata => {
                    layer.msg("添加成功");
                    self.tablereldept.reload();
                })
            });

            //监听行工具事件
            layui.table.on('tool(RelDeptTable)', function (obj) {  //tool是工具条事件名，DeptInfoTable是table原始容器的属性
                var data = obj.data;//当前行数据
                if (obj.event === "enabledept") {
                    layer.confirm("确定设置为默认科室吗?", { icon: 3, title: "提示" },
                        function (index) {
                            lobsterlayui.GET('/admin/v1/emp/SetDefaultDept', { empId: empid, deptId: data.DeptId }).then(retdata => {
                                layer.msg("设置成功");
                                self.tablereldept.reload();
                            })
                        });
                }
            });
        },
        loadselectdept(workid) {
            //获取机构数据
            lobsterlayui.GET('/admin/v1/emp/GetDeptSelectData', { workId: workid }).then(data => {
                //医疗机构
                var deptdata = data["selectdata"];
                $("#dlg_deptId").html("");
                $.each(deptdata, function (i, item) {
                    $("#dlg_deptId").append("<option value=\"" + item.DeptId + "\">" + item.BranchHospitalName + "|" + item.LayerName + "|" + item.Name + "</option>");
                });

                $('#dlg_deptId').val(deptdata[0].DeptId);//默认选择当前登录机构
                //重新渲染
                layui.form.render("select");
            })
        },
        loadtablereldept(empid) {
            this.tablereldept= layui.table.render({
                elem: '#RelDeptTable',
                headers: lobsterlayui.GetHeaderData(),
                where: {
                    empId: empid, //人员ID
                },
                height: 'full',
                cellMinWidth: 80,
                url: '/admin/v1/emp/GetEmpRelDepts',
                //even: true,

                cols: [[
                    { title: '设置默认科室', width: '150', fixed: 'left',  templet: '#defaultdept' },
                    { field: 'DeptId', title: '编码', width: '80' },
                    { field: 'Name', title: '科室名称', width: '100' },
                    { field: 'LayerName', title: '科室分类', width: '120' },
                    { field: 'BranchHospitalName', title: '所属分院', width: '120' }
                ]],
                parseData: function (res) {
                    return {
                        code: res.code,
                        msg: res.msg,
                        count: res.data.count,
                        data: res.data.list
                    }
                }
            })
        }
    }
    //授权应用
    var authapp = {
        initpage(data) {
            this.loadtreeapprole(data.EmpId, data.WorkId);
            //this.initevent();
        },
        initevent() {
            //监听角色授权界面按钮事件函数
            var active = {
                spread: function () {
                    authtree.showAll('#LAY-auth-tree-index');
                }
                , fold: function () {
                    authtree.closeAll('#LAY-auth-tree-index');

                }
                , checkall: function () {
                    authtree.checkAll('#LAY-auth-tree-index');
                }
                , uncheckall: function () {
                    authtree.uncheckAll('#LAY-auth-tree-index');
                }
                , refresh: function () {
                    loadtreeapprole();//加载权限树
                }
            };
            //监听（展开、折叠、刷新）
            $('.layui-btn').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        },
        loadtreeapprole(empId,workId) {

            lobsterlayui.GET('/admin/v1/emp/GetAppRole', { empId: empId, workId: workId }).then(retdata => {
                layui.tree.render({
                    elem: '#LAY-auth-tree-index'
                    ,id:'authtree'
                    , data: retdata["treedata"]
                    , showCheckbox: true  //是否显示复选框
                    , isJump: true //是否允许点击节点时弹出新窗口跳转
                    , click: function (obj) {
                        //var data = obj.data;  //获取当前点击的节点数据
                        //layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
                    }
                });
            })
        },
        savetreeapprole(data) {
            var checkedData = layui.tree.getChecked('authtree'); //获取选中节点的数据
            var groupIds = [];
            checkedData.forEach(item => {
                item.children.forEach(g => {
                    groupIds.push(g.id);
                })
            });
            var load = layer.msg('正在处理，请稍候', { icon: 16, time: 0, shade: [0.3, '#393D49'] });
            lobsterlayui.GET('/admin/v1/emp/SaveAppRole', { empId: data.EmpId, workId: data.WorkId, groupIds: groupIds.join(',') }).then(retdata => {
                layer.closeAll();
                layer.msg("保存成功");
            })
        }
    }

    /**
    * 关键信息隐藏
    * @param str 字符串
    * @param frontLen 字符串前面保留位数
    * @param endLen 字符串后面保留位数
    * @returns {string}
    */
    var hideCode= function (str, frontLen, endLen) {
        var len = str.length - frontLen - endLen;
        var xing = '';
        for (var i = 0; i < len; i++) {
            xing += '*';
        }
        return str.substring(0, frontLen) + xing + str.substring(str.length - endLen);
    }
</script>

<script type="text/html" id="flagTpl">
    {{#  if(d.StrDelFlag == '已停用'){ }}
    <span style="color:red;">{{ d.StrDelFlag }}</span>
    {{#  } else { }}
    {{ d.StrDelFlag }}
    {{#  } }}
</script>
<script type="text/html" id="dateTpl">
    {{ layui.util.toDateString(new Date(d.Brithday).getTime(), 'yyyy-MM-dd') }}
</script>
<script type="text/html" id="IDNumberTpl">
    {{ hideCode(d.IDNumber,4,4) }}
</script>
<script type="text/html" id="TelephoneTpl">
    {{ hideCode(d.Telephone,3,2) }}
</script>
<script type="text/html" id="useroption">
    <a class="layui-btn layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
    <a class="layui-btn layui-btn-xs" lay-event="authapp"><i class="layui-icon layui-icon-link"></i>授权应用</a>
    <a class="layui-btn layui-btn-xs" lay-event="reldept"><i class="layui-icon layui-icon-link"></i>关联科室</a>
    {{#  if(d.StrDelFlag == '已启用'){ }}
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="disable"><i class="layui-icon layui-extend-stop"></i>停用</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enable"><i class="layui-icon layui-icon-ok"></i>启用</a>
    {{#  } }}
</script>

<script type="text/html" id="dlg_addemp">
    <div class="layui-fluid box">

        <form class="layui-form" lay-filter="formEmps" id="formEmps" name="formEmps">
            <div class="layui-card">
                <div class="layui-card-body box" id="EmpInfo">
                    <div class="layui-row">
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px"><i style="color:red;">*&ensp; </i>姓名</label>
                                <div class="layui-input-block">
                                    <input type="text" name="Name" lay-verify="required" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px"><i style="color:red;">*&ensp; </i>身份证号</label>
                                <div class="layui-input-block">
                                    <input type="text" id="IDNumber" name="IDNumber" required lay-verify="chkIDNumber" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">性别</label>
                                <div class="layui-input-block">
                                    <select name="Sex" id="Sex" lay-filter="sex"></select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">出生日期</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" id="Brithday" name="Brithday" placeholder="yyyy-MM-dd">
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px"><i style="color:red;">*&ensp; </i>联系电话</label>
                                <div class="layui-input-block">
                                    <input type="text" id="Telephone" name="Telephone" lay-verify="chkphone" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                        </div>


                        <!--<div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">默认科室</label>
                                <div class="layui-input-block">
                                    <input type="text" class="layui-input" name="DefaultDept" id="DefaultDept" />

                                </div>
                            </div>
                        </div>-->

                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">人员类别</label>
                                <div class="layui-input-block" id="divUserType">
                                    <select name="EmpType" id="EmpType" lay-filter="EmpType"></select>
                                </div>
                            </div>
                        </div>

                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">简介</label>
                                <div class="layui-input-block">
                                    <textarea class="layui-textarea" id="Remind" name="Remind" placeholder="请输入简介"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12">
                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">擅长</label>
                                <div class="layui-input-block">
                                    <textarea class="layui-textarea" id="Speciality" name="Speciality" placeholder="请输入擅长"></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="layui-col-xs12">

                            <div class="layui-form-item">
                                <label class="layui-form-label" style="width:100px">头像</label>
                                <div class="layui-input-block">
                                    <input type="hidden" id="UPic" name="UPic">
                                    <div class="layui-upload">
                                        <div class="layui-inline">
                                            <button type="button" class="layui-btn" id="btnUpload">选择图片</button>
                                            <i style="color:#999;">图片大小不超过1M</i>
                                        </div>
                                        <div class="layui-upload-list">
                                            <img class="layui-upload-img" name="imgUpload" id="imgUpload" style="width:90px; height:90px">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="layui-form-item layui-hide">
                            <input type="text" name="EmpId" id="EmpId" value="0">
                            <input type="text" name="empDeptId" id="empDeptId" value="0">
                            <input type="text" name="WorkId" id="WorkId">
                        </div>
                        <div class="layui-form-item layui-hide">
                            <input type="button" lay-submit lay-filter="btnSubmit" id="btnSubmit" value="保存">
                            <input type="reset" id="LAY-emp-form-reset" value="重置">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
</script>

<script type="text/html" id="dlg_reldept">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style=" width: 100px; ">科室：</label>
                        <div class="layui-input-block">
                            <select name="deptId" id="dlg_deptId" lay-filter="dlg_deptId"></select>
                        </div>
                    </div>
                </div>
                <div class="layui-input-inline">
                    <div class="layui-form-item">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="btnAddDept" id="btnAddDept">
                            <i class="layui-icon layuiadmin-button-btn layui-icon-add-1"></i>添加科室
                        </button>
                    </div>
                </div>
            </div>
            <div class="layui-card-body">
                <table id="RelDeptTable" lay-filter="RelDeptTable"></table>
            </div>
        </div>
    </div>
</script>

<script type="text/html" id="defaultdept">
    {{# if(d.DefaultFlag==1){ }}
    <label style="color:red">是</label>
    {{# }else{ }}
    <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="enabledept"><i class="layui-icon layui-icon-ok"></i>设为默认</a>
    {{# } }}
</script>


<script type="text/html" id="dlg_authapp">
        <div class="layui-layout">
            
            <div id="LAY-auth-tree-index"></div>
            
        </div>
</script>